import uiautomation as uia



def print_ui_tree(app_name):
    # 获取指定应用的窗口
    app_window = uia.WindowControl(searchDepth=1, Compare=window_cmp)
    if not app_window.Exists():
        print(f"找不到应用窗口: {app_name}")
        raise Exception("剪映窗口未找到")
    app_window.SetActive()
    app_window.SetTopmost()

    # 打印窗口的 UI 树结构
    def print_control_tree(control, indent=0):
        print('  ' * indent + f"{control.ControlType} - {control.Name}")
        for child in control.GetChildren():
            print_control_tree(child, indent + 1)

        # 查找特定类名的控件

    def find_control_by_class_name(control, target_class_name):
        if control.ClassName == target_class_name:
            return control
        for child in control.GetChildren():
            result = find_control_by_class_name(child, target_class_name)
            if result:
                return result
        return None
    # 找到这个class_name Compare=ControlFinder.class_name_matcher("PanelSettingsGroup_QMLTYPE"))
    print_control_tree(app_window)


app_status = ""
def window_cmp(control: uia.WindowControl, depth: int) -> bool:
    if control.Name != "剪映专业版":
        return False
    if "HomePage".lower() in control.ClassName.lower():
        app_status = "home"
        return True
    if "MainWindow".lower() in control.ClassName.lower():
        app_status = "edit"
        return True
    return False


if __name__ == "__main__":
    # 替换为你要打印的应用窗口名称
    app_name = "剪影专业版"
    print_ui_tree(app_name)
